package com.ftm.dao.impl;

import java.util.List;

import com.ftm.dao.UserDao;
import com.ftm.entity.User;
import com.ftm.tools.DBTools;
import com.ftm.tools.PageData;

/**
 * 用户操作dao
 * 
 * @author zcl
 *
 */
public class UserDaoImpl implements UserDao {

	@Override
	public User userLogin(String username, String userpwd) {
		String sql = "select * from ftm_user where username=? and userpwd=? and userstate=1";
		List<User> list = (List<User>) DBTools.exQuery(sql, User.class, username, userpwd);
		// list里有记录，登录成功
		if (list.size() > 0) {
			return list.get(0);
		}
		// 登录失败
		return null;
	}

	@Override
	public boolean addUser(User user) {
		String sql = "insert into ftm_user values(null,?,?,?,?,?)";
		int n = DBTools.exUpdate(sql, user.getUsername(), user.getUserpwd(), user.getUserphone(), user.getUserstate(),
				user.getAddress());
		// 新用户注册成功
		if (n > 0)
			return true;
		return false;
	}

	@Override
	public boolean proUser(String username, int userstate) {
		String sql = "update ftm_user set userstate=? where username=?";
		int n = DBTools.exUpdate(sql, userstate, username);
		// 用户状态修改成功
		if (n > 0)
			return true;
		return false;
	}

	@Override
	public boolean delUser() {
		String sql = "delete from ftm_user where userstate=0";
		int n = DBTools.exUpdate(sql);
		// 删除被封禁的用户
		if (n > 0)
			return true;
		return false;
	}

	@Override
	public PageData<User> selUserByPage(int page, int pageSize, String key) {
		String sql = "select * from ftm_user where username like ?";
		PageData<User> pd = DBTools.exQueryByPage(sql, User.class, page, pageSize, "%" + key + "%");
		return pd;
	}

	@Override
	public boolean modUser(int uid, String username, String userpwd, String userphone, String address) {
		String sql = "update ftm_user set username=?,userpwd=?,userphone=?,address=? where userid=?";
		int n = DBTools.exUpdate(sql, username, userpwd, userphone, address, uid);
		// 用户状态修改成功
		if (n > 0)
			return true;
		return false;
	}

	/**
	 * 郑志伟
	 */

	@Override
	public User getUserByName(String username) {
		// TODO Auto-generated method stub
		String sql = "select * from ftm_user where username = ?";
		List<User> list = (List<User>) DBTools.exQuery(sql, User.class, username);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<User> getUser(String keywords) {
		// TODO Auto-generated method stub
		String sql = "select * from ftm_user where username like ?";
		List<User> list = (List<User>) DBTools.exQuery(sql, User.class, "%" + keywords + "%");
		return list;
	}


}
